
package com.companymodule.servlet;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.UtilDAO;

import com.companymodule.bean.TbCompany;
import com.companymodule.dao.TbCompanyDAO;
import com.companytypemodule.dao.TbCompanyTypeDAO;
import com.creditratingmodule.dao.TbCreditRatingDAO;
import com.creditratingtypemodule.dao.TbCreditRatingTypeDAO;

public class TbCompanyServlet extends HttpServlet{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        request.getSession(true).setAttribute("tbCompanyTypeList", new TbCompanyTypeDAO().getTbCompanyTypeAll());
        request.getSession(true).setAttribute("tbCreditRatingList", new TbCreditRatingDAO().getTbCreditRatingAll());
        
        if("add".equals(method)){
        String companyCode=request.getParameter("companyCode");
        String companyTypeIdAll [] = request.getParameterValues("companyTypeId");
        String companyTypeId="";
        for(String typeId :companyTypeIdAll){
    		companyTypeId="".equals(companyTypeId)?typeId:companyTypeId+","+typeId;
    	}
        String companyName=request.getParameter("companyName");
        String parentCompanyName=request.getParameter("parentCompanyName");
        String companyAddress=request.getParameter("companyAddress");
        String companyTelphone=request.getParameter("companyTelphone");
        String companyMobile=request.getParameter("companyMobile");
        String companyCreateTime=request.getParameter("companyCreateTime");
        int companyManCount=Integer.parseInt(request.getParameter("companyManCount"));
        String companyContactList=request.getParameter("companyContactList");
        int creditRatingId=Integer.parseInt(request.getParameter("creditRatingId")==null?"0":request.getParameter("creditRatingId"));
        String companyMemo=request.getParameter("companyMemo");
        System.out.println("companyTypeId=="+companyTypeId);
            if(new TbCompanyDAO().add(new TbCompany(companyCode,companyTypeId,companyName,parentCompanyName,companyAddress,companyTelphone,companyMobile,companyCreateTime,companyManCount,companyContactList,creditRatingId,companyMemo,new UtilDAO().decodeDate()))){
            request.getSession().setAttribute("result", "添加成功");
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
             request.getSession(true).setAttribute("tbCompanyList",new TbCompanyDAO().getTbCompanyAll());
            response.sendRedirect("CompanyModule/TbCompanyList.jsp");
        }
        if("del".equals(method)){
            if(UtilDAO.delin("Tb_Company", "CompanyId", request.getParameter("delIds"))){            
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
             request.getSession(true).setAttribute("tbCompanyList",new TbCompanyDAO().getTbCompanyAll());
            response.sendRedirect("CompanyModule/TbCompanyList.jsp");
        }
        if("edit".equals(method)){
        int companyId=Integer.parseInt(request.getParameter("companyId"));
        String companyCode=request.getParameter("companyCode");
        String companyTypeIdAll [] = request.getParameterValues("companyTypeId");
        String companyTypeId="";
        for(String typeId :companyTypeIdAll){
    		companyTypeId="".equals(companyTypeId)?typeId:companyTypeId+","+typeId;
    	}
        String companyName=request.getParameter("companyName");
        String parentCompanyName=request.getParameter("parentCompanyName");
        String companyAddress=request.getParameter("companyAddress");
        String companyTelphone=request.getParameter("companyTelphone");
        String companyMobile=request.getParameter("companyMobile");
        String companyCreateTime=request.getParameter("companyCreateTime");
        int companyManCount=Integer.parseInt(request.getParameter("companyManCount"));
        String companyContactList=request.getParameter("companyContactList");
        int creditRatingId=Integer.parseInt(request.getParameter("creditRatingId")==null?"0":request.getParameter("creditRatingId"));
        String companyMemo=request.getParameter("companyMemo");
            if(new TbCompanyDAO().edit(new TbCompany(companyId,companyCode,companyTypeId,companyName,parentCompanyName,companyAddress,companyTelphone,companyMobile,companyCreateTime,companyManCount,companyContactList,creditRatingId,companyMemo,new UtilDAO().decodeDate()))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
             request.getSession(true).setAttribute("tbCompanyList",new TbCompanyDAO().getTbCompanyAll());
            response.sendRedirect("CompanyModule/TbCompanyList.jsp");
        }
        if ("list".equals(method)) {
        	request.getSession().setAttribute("tbCompanyTypeListAll", new TbCompanyTypeDAO().getTbCompanyTypeAll());
             request.getSession(true).setAttribute("tbCompanyList",new TbCompanyDAO().getTbCompanyAll());
            response.sendRedirect("CompanyModule/TbCompanyList.jsp");
        	}
        if ("listById".equals(method)) {
            request.getSession(true).setAttribute("tbCompany",new TbCompanyDAO().getTbCompanyByCompanyId(Integer.parseInt(request.getParameter("companyId"))));
            response.sendRedirect("CompanyModule/TbCompanyOperate.jsp");
    	}
        if("showadd".equals(method)){
            response.sendRedirect("CompanyModule/TbCompanyOperate.jsp");
        }
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doGet
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doPost
}
